//
//  KLKuaidiViewController.m
//  KL
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 zhouxubin. All rights reserved.
//
#define KLKuaidiKey @"1904d99dc7a6beae58793bd2bd3f2d34"
#define KLKuaidiAPI @"http://v.juhe.cn/exp/index"

#import "KLKuaidiViewController.h"
#import "ZBNetWorkTool.h"
#import "KLKuaidi.h"
#import "KLKuaidiDetailController.h"
#import <SVProgressHUD.h>

@interface KLKuaidiViewController ()
@property (weak, nonatomic) IBOutlet UITextField *companyName;
@property (weak, nonatomic) IBOutlet UITextField *order;
@property (nonatomic, strong) KLKuaidi *kuaidi;
@property (weak, nonatomic) IBOutlet UIButton *checkButton;

@end

@implementation KLKuaidiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置按钮的圆角
    self.checkButton.layer.cornerRadius = 10;
    self.checkButton.layer.masksToBounds = YES;
    
    // 设置背景颜色
//    self.view.backgroundColor = KLBackgroundColor;
}

#pragma mark - 生命周期方法
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self.companyName becomeFirstResponder];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

#pragma mark - 查询快递按钮的点击事件
- (IBAction)checkClick {
    [self.view endEditing:YES];
    // 判断
    if (self.companyName.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入快递公司名称"];
        return;
    }
    
    if (self.order.text.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请输入快递单号"];
        return;
    }
    
    // 发送请求,并解析数据
    NSMutableDictionary *params = [NSMutableDictionary dictionary];
    params[@"com"] = [self getCorrectCompanyNameWith:self.companyName.text];
    params[@"no"] = self.order.text;
    params[@"key"] = KLKuaidiKey;
    [[ZBNetWorkTool sharedNetWorkTool] GET:KLKuaidiAPI parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *responseObject) {
        if ([responseObject[@"reason"] isEqualToString:@"查询成功"]) {
            // 字典转模型
            NSDictionary *dict = responseObject[@"result"];
            KLKuaidi *kuaidi = [KLKuaidi mj_objectWithKeyValues:dict];
            self.kuaidi = kuaidi;
            // 请求成功后才跳转
            // 取出目标控制器
            KLKuaidiDetailController *kuaidiDetailVc = [[KLKuaidiDetailController alloc] init];
            kuaidiDetailVc.kuaidi = kuaidi;
            [self.navigationController pushViewController:kuaidiDetailVc animated:YES];
        }else {
            [SVProgressHUD showErrorWithStatus:@"查询不到您的快递,检查下您的输入是否正确~"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        KLLog(@"%@", error);
        [SVProgressHUD showErrorWithStatus:@"查询不到您的快递哟,检查下您的输入是否正确哟~"];
    }];
}

- (NSString *)getCorrectCompanyNameWith:(NSString *)companyName {
    // EMS、圆通、顺丰、申通、中通、韵达、天天
    if ([companyName containsString:@"EMS"]) {
        return @"ems";
    }else if ([companyName containsString:@"圆通"]) {
        return @"yt";
    }else if ([companyName containsString:@"顺丰"]) {
        return @"sf";
    }else if ([companyName containsString:@"申通"]) {
        return @"sto";
    }else if ([companyName containsString:@"中通"]) {
        return @"zto";
    }else if ([companyName containsString:@"韵达"]) {
        return @"yd";
    }else if ([companyName containsString:@"天天"]) {
        return @"tt";
    }
    return @"没有匹配";
}

@end
